package com.cade.agriculturemall.network;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Parcelable;
import android.text.GetChars;
import android.util.Log;
import android.widget.Toast;

import com.cade.agriculturemall.base.BaseActivity;
import com.cade.agriculturemall.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hshunxin on 2018/2/7.
 */

/**
 * 自定义检查手机网络状态是否切换的广播接受器
 *
 * @author cj
 */
public class NetBroadcastReceiver extends BroadcastReceiver {
//    private static NetBroadcastReceiver netBroadcastReceiver;
//    public List<OnNetWorkChange> onNetWorkChange = new ArrayList<>();

    private String getConnectionType(int type) {
        String connType = "";
        if (type == ConnectivityManager.TYPE_MOBILE) {
            connType = "3G网络数据";
        } else if (type == ConnectivityManager.TYPE_WIFI) {
            connType = "WIFI网络";
        }
        return connType;
    }

//    public static NetBroadcastReceiver getInstance() {
//        if (netBroadcastReceiver == null) {
//            netBroadcastReceiver = new NetBroadcastReceiver();
//        }
//        return netBroadcastReceiver;
//    }
//
//    //回调接口
//    public interface OnNetWorkChange {
//        //返回各个（wifi,移动网络，没有网络）状态的值，上一个网络状态的值，当前的网络状态的值
//        void onChange(boolean isConnect);
//    }
//
//    /**
//     * 增加网络变化监听回调对象
//     * 如果设置多个回调，请务必不要设置相同名字的OnNetWorkChange对象，否则会无效
//     *
//     * @param onNetWorkChange 回调对象
//     */
//    public void setOnNetWorkChange(OnNetWorkChange onNetWorkChange) {
//        if (this.onNetWorkChange.contains(onNetWorkChange)) {
//            return;
//        }
//        this.onNetWorkChange.add(onNetWorkChange);
//        Log.i("网络状态", "添加一个回调。已设置：" + this.onNetWorkChange.size());
//    }
//
//    /**
//     * 取消网络变化监听监听回调
//     *
//     * @param onNetWorkChange 回调对象
//     */
//    public void delOnNetWorkChange(OnNetWorkChange onNetWorkChange) {
//        if (this.onNetWorkChange.contains(onNetWorkChange)) {
//            this.onNetWorkChange.remove(onNetWorkChange);
//            Log.i("网络状态", "删除一个回调。还有：" + this.onNetWorkChange.size());
//        }
//    }
//    /**
//     * 触发网络状态监听回调
//     *
//     * @param isConnect 当前网络连接状态
//     */
//    private void setChange(boolean isConnect) {
//        for (OnNetWorkChange change : onNetWorkChange) {
//            change.onChange(isConnect);
//        }
////        oldState = nowStatus;
//    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Context mContext = context;
        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
            // 监听wifi的打开与关闭，与wifi的连接无关
            int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
            Log.e("TAG", "wifiState:" + wifiState);
            switch (wifiState) {
                case WifiManager.WIFI_STATE_DISABLED:
                    break;
                case WifiManager.WIFI_STATE_DISABLING:
                    break;
            }
        } // 监听wifi的连接状态即是否连上了一个有效无线路由
        if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
            Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (null != parcelableExtra) { //
                // 获取联网状态的NetWorkInfo对象
                NetworkInfo networkInfo = (NetworkInfo) parcelableExtra; //获取的State对象则代表着连接成功与否等状态
                NetworkInfo.State state = networkInfo.getState(); //判断网络是否已经连接
                boolean isConnected = state == NetworkInfo.State.CONNECTED;
                Log.e("TAG", "isConnected:" + isConnected);
                if (isConnected) {
//                    setChange(isConnected);
//                    Toast.makeText(mContext, "NetworkInfo.State:" + isConnected, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(mContext, "网络状态不稳定" + isConnected, Toast.LENGTH_SHORT).show();
                }
            }
        }
        // 监听网络连接，包括wifi和移动数据的打开和关闭,以及连接上可用的连接都会接到监听
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            // 获取联网状态的NetworkInfo对象
            NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (info != null) {
                // 如果当前的网络连接成功并且网络连接可用
                if (NetworkInfo.State.CONNECTED == info.getState() && info.isAvailable()) {
                    if (info.getType() == ConnectivityManager.TYPE_WIFI || info.getType() == ConnectivityManager.TYPE_MOBILE) {
                        Log.i("TAG", getConnectionType(info.getType()) + "连上");
                        Toast.makeText(mContext, getConnectionType(info.getType()) + "连上", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Log.i("TAG", getConnectionType(info.getType()) + "断开");
                    Toast.makeText(mContext, getConnectionType(info.getType()) + "断开", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}

//        作者：xlijuan
//        链接：https://www.jianshu.com/p/77825d3d6393
//        來源：简书
//        著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。